package com.sctvcloud.dongqu.ui.utils;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.fragment.BaseGeneraFragment;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.NewsDate;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.beans.Titles;
import com.sctvcloud.dongqu.http.AbsListNetCallback;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.adapter.GreatestAdapter;
import com.sctvcloud.dongqu.ui.util.IListShowData;
import com.sctvcloud.dongqu.utils.ListTypeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyu on 2017/8/18.
 */

public class MutilDateNewsGetHelper {

    public static final int ERROR_NO_TODAY = 200001;
    public static final int ERROR_NO_YESTERDAY = 200002;
    public static final int ERROR_NO_GETTING = 20001;

    List<NewsDate> dates;
    private boolean shouldSetTitleAfter;
    private boolean hasAddFirtDateTitle;
    private boolean isContinue;
    private boolean isGeting;

    private NewsDate currentDate;
    private int currentIndex = -1;

    private final String ownerName;

    public MutilDateNewsGetHelper(BaseActivity activity) {
        ownerName = activity + "";
    }

    public MutilDateNewsGetHelper(BaseGeneraFragment fragment) {
        ownerName = fragment + "";
    }

    public int getNewsList(int size, boolean reset, AbsListNetCallback<IListShowData> callback) {
        int re = -1;
        if (isGeting) {
            if (callback != null) {
                callback.onError(null, "this is in gettting", ERROR_NO_GETTING);
                callback.onEnd();
            }
            return re;
        }
        if (!ListUtils.isListValued(dates)) {
            if (callback != null) {
                callback.onError(null, "this is no dates", ERROR_NO_GETTING);
                callback.onEnd();
            }
            return re;
        }
        isGeting = true;
        if (reset) {
            reset();
        }
        if (currentDate == null || currentDate.hasGettedAll()) {
            if (nextDate()) {
                getDateNews(size, null, callback);
            } else {
                re = 0;
            }
        } else {
            re = 1;
            getDateNews(size, null, callback);
        }
        return re;
    }

    private void getDateNews(final int size, final List<IListShowData> list, AbsListNetCallback<IListShowData> callback) {
        NetUtils.getNetAdapter().getNewsPage(ownerName, currentDate.getNewsListUrl(), currentDate.getIndex() + 1
                , new AbsListNetCallback<NewsItem>(ListTypeUtils.NewsItem(), callback, currentDate.getIndex() + 1) {
                    private ArrayList<IListShowData> lis;

                    @Override
                    public void onSuc(List<NewsItem> newsItems) {
                        currentDate.setIndex(calledIndex);
                        currentDate.setTotal(getPageResponse().getData().getPageAll());
                        int to = ListUtils.getTotalSize(lis, list, calledIndex == 0 ? -2 : -1);
                        if (size > 0 && size > to) {
                            isContinue = !currentDate.hasGettedAll() || nextDate();
                            if (isContinue) {
                                if (list != null) {
                                    list.addAll(lis);
                                    getDateNews(size, list, (AbsListNetCallback) obj);
                                } else {
                                    getDateNews(size, lis, (AbsListNetCallback) obj);
                                }
                            } else {
                                if (list != null) {
                                    list.addAll(lis);
                                    ((AbsListNetCallback) obj).onSuc(list);
                                } else {
                                    ((AbsListNetCallback) obj).onSuc(lis);
                                }
                            }
                        } else {
                            if (lis != null) {
                                list.addAll(lis);
                                ((AbsListNetCallback) obj).onSuc(list);
                            } else {
                                ((AbsListNetCallback) obj).onSuc(lis);
                            }
                        }
                    }

                    @Override
                    public List<NewsItem> doInBackground(List<NewsItem> newsItems) {
                        lis = new ArrayList<>();
                        if (ListUtils.isListValued(newsItems)) {
                            if (calledIndex == 0) {
//                                JLog.e("will add title and has add first " + hasAddFirtDateTitle + "\n " + MutilDateNewsGetHelper.this);
                                Titles t = new Titles();
                                t.setDateString(DateUtils.formatDate(DateUtils.getDataDate(currentDate), "MM月dd日"));
                                t.setViewType(hasAddFirtDateTitle ? GreatestAdapter.VIEWTYPE_TITLE_24_CONTINUE : GreatestAdapter.VIEWTYPE_TITLE_24);
                                if (shouldSetTitleAfter) {
                                    newsItems.get(0).setAfterTitleMode(hasAddFirtDateTitle ? 2 : 1);
                                }
                                hasAddFirtDateTitle = true;
                                lis.add(t);
                            }
                            lis.addAll(newsItems);
                        }
                        return super.doInBackground(newsItems);
                    }

                    @Override
                    public void onError(Throwable ex, String errorMsg, int netCode) {
                        int to = ListUtils.getTotalSize(lis, list, calledIndex == 0 ? -2 : -1);
                        if (size > 0 && size > to) {
                            //根据日期请求新闻时，如果某一天的新闻获取数据失败，就继续请求下一个日期的。
                            isContinue = !currentDate.hasGettedAll() || nextDate();
                            if (isContinue) {
                                if (list != null) {
                                    getDateNews(size, list, (AbsListNetCallback) obj);
                                } else {
                                    getDateNews(size, lis, (AbsListNetCallback) obj);
                                }
                            }
                        } else if (list != null) {
                            ((AbsListNetCallback) obj).onSuc(list);
                        }
                        ((AbsListNetCallback) obj).onError(ex, errorMsg, netCode);
                    }

                    @Override
                    public void onEnd() {
                        super.onEnd();
//                        JLog.e("in yesterday end is continue=" + isContinue);
                        if (isContinue) {
                            isContinue = false;
                        } else {
                            isGeting = false;
                            ((AbsListNetCallback) obj).onEnd();
                        }
                    }
                });
    }

    private boolean nextDate() {
        if (currentIndex < -1) {
            currentIndex = 0;
        } else if (currentIndex >= dates.size() - 1) {
            return false;
        } else {
            currentIndex++;
        }
        currentDate = dates.get(currentIndex);
        return true;
    }

    private boolean isCurrnetGetedAll() {
        return currentDate != null && currentDate.hasGettedAll();
    }

    private void reset() {
        currentDate = null;
        currentIndex = -1;
        isContinue = false;
        hasAddFirtDateTitle = false;
    }

    public void setDates(List<NewsDate> dates) {
        this.dates = dates;
    }

    public void setShouldSetTitleAfter(boolean shouldSetTitleAfter) {
        this.shouldSetTitleAfter = shouldSetTitleAfter;
    }

    public boolean hasGetAll() {
        return !ListUtils.isListValued(dates) || (currentIndex >= dates.size() - 1 && isCurrnetGetedAll());
    }

    public boolean hasValuedDate() {
        return ListUtils.isListValued(dates);
    }
}
